<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>作者博客：百度"three.js 郭隆邦"</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
      /* 隐藏body窗口区域滚动条 */
    }
  </style>
  <!--引入three.js三维引擎-->
  <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
  <!-- 引入threejs扩展控件OrbitControls.js -->
  <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/examples/js/controls/OrbitControls.js"></script>
</head>

<body>
  <script>
    /**
     * 创建场景对象Scene
     */
    var scene = new THREE.Scene();
    /**
     * 创建网格模型
     */
    // var geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
    var geometry = new THREE.BoxGeometry(100, 100, 100);
    var material = new THREE.ShaderMaterial({
      // 调用UniformsLib.js文件中的uniform变量代码块
      uniforms: THREE.UniformsUtils.merge([
      THREE.UniformsLib.common,
      THREE.UniformsLib.specularmap,
      THREE.UniformsLib.envmap,
      THREE.UniformsLib.aomap,
      THREE.UniformsLib.lightmap,
      THREE.UniformsLib.emissivemap,
      THREE.UniformsLib.bumpmap,
      THREE.UniformsLib.normalmap,
      THREE.UniformsLib.displacementmap,
      THREE.UniformsLib.gradientmap,
      THREE.UniformsLib.fog,
      THREE.UniformsLib.lights,
      {
        emissive: { value: new THREE.Color( 0x000000 ) },
        specular: { value: new THREE.Color( 0x111111 ) },
        shininess: { value: 30 }
      }
      ]),
      // 获得ShaderLib文件下着色器文件meshphong_vert.glsl代码
      vertexShader: THREE.ShaderChunk.meshphong_vert,
      // 获得ShaderLib文件下着色器文件meshphong_frag.glsl代码
      fragmentShader: THREE.ShaderChunk.meshphong_frag
    });

    // phong受光照影响,ShaderMaterial的lights属性需要设置为true，默认是false
    material.lights = true;
    // 设置材质颜色
    material.uniforms.diffuse.value.setRGB(1.0, 1.0, 0.0)

    /**
     * 光源设置
     */
    //点光源
    var point = new THREE.PointLight(0xffffff,0.5);
    point.position.set(400, 200, 300); //点光源位置
    scene.add(point); //点光源添加到场景中
    //环境光
    var ambient = new THREE.AmbientLight(0xffffff, 0.5);
    scene.add(ambient);
    // 方向光
    var directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
    directionalLight.position.set(-1, -1, 1);
    scene.add(directionalLight);

    var mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);
    // 网格模型旋转操作
    mesh.rotateY(Math.PI/6);
    mesh.rotateX(Math.PI/6);
    /**
     * 相机设置
     */
    var width = window.innerWidth; //窗口宽度
    var height = window.innerHeight; //窗口高度
    var k = width / height; //窗口宽高比
    var s = 150; //三维场景显示范围控制系数，系数越大，显示的范围越大
    //创建相机对象
    var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    camera.position.set(200, 300, 200); //设置相机位置
    camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
    /**
     * 创建渲染器对象
     */
    var renderer = new THREE.WebGLRenderer({antialias: true});
    renderer.setSize(width, height); //设置渲染区域尺寸
    document.body.appendChild(renderer.domElement); //body元素中插入canvas对象

    // 渲染函数
    function render() {
      renderer.render(scene, camera); //执行渲染操作
      requestAnimationFrame(render); //请求再次执行渲染函数render，渲染下一帧
    }
    render();
    //创建控件对象  相机对象camera作为参数   控件可以监听鼠标的变化，改变相机对象的属性
    var controls = new THREE.OrbitControls(camera,renderer.domElement);
  </script>
</body>

</html>
